#include "StructureDonnees.hpp"

#include <iostream>

using namespace std;

StructureDonnees::StructureDonnees()
{}

StructureDonnees::~StructureDonnees()
{}

int StructureDonnees::nombreElements() const
{
  return size();
}

int StructureDonnees::nombreElements( const QString& nomElement ) const
{
  BaseStructure::const_iterator pos( find( nomElement ) ),
    fin( end() );

  if ( fin == begin() )
    return -1;

  int nombre( 0 );

  for ( ; pos != fin; ++pos )
    {
      if ( pos . key() != nomElement )
	break;
      nombre++;
    }

  return nombre;
}

const QHash< QString, QString >*
StructureDonnees::listeAttributs( const QString& nomElement,
				  const int& numero ) const
{
   BaseStructure::const_iterator pos( find( nomElement ) ),
    fin( end() );

   if ( fin == begin() )
     return 0;
   else if ( pos == fin )
     return 0;

   int nbr( nombreElements( nomElement ) );

   if ( numero >= nbr )
     return 0;

   for ( nbr = 0; nbr < numero; nbr++ )
     ++pos;

   return &( pos . value() );
}

QString StructureDonnees::valeurAttribut( const QString& nomElement,
					  const QString& nomAttribut,
					  const int& numero ) const
{
  const QHash< QString, QString >* attributs =
    listeAttributs( nomElement, numero );

  if ( attributs == 0 )
    return QString();

  QHash< QString, QString >::const_iterator
    pos( attributs -> find( nomAttribut ) );

  if ( pos == attributs -> end() )
    return QString();

  return pos . value();
}

void StructureDonnees::afficheContenu() const
{
  BaseStructure::const_iterator it, debut( begin() ), fin( end() );

  QString tmpClef;

  for ( it = debut; it != fin; ++it )
    {
      cout << it . key() . toUtf8() . constData() << " :" << endl;

      foreach ( tmpClef, it . value() . keys() )
        cout << " - " << tmpClef . toUtf8() . constData() << " <=> "
             << it . value() [ tmpClef ] . toUtf8() . constData() << endl;
    }
}
